<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>添加应用</title>
    <link rel="stylesheet" th:href="@{/static/lib/layui/css/layui.css}" media="all"/>
    <style>

    </style>
</head>
<body style="padding: 30px 45px 30px 15px">


<form class="layui-form" id="add_app_form">
    <div class="layui-form-item">
        <label class="layui-form-label">应用名：</label>
        <div class="layui-input-block">
            <input type="text" name="appName" placeholder="请输入" class="layui-input" lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">日志路径：</label>
        <div class="layui-input-block">
            <input type="text" name="logPath" placeholder="请输入" class="layui-input" lay-verify="required">
        </div>
    </div>
    <button type="button" class="layui-btn" id="submit" style="margin-top: 25px;float: right;width: 90px;">创建</button>
</form>

<script th:src="@{/static/lib/layui/layui.js}"></script>
<script type="text/javascript" th:inline="javascript">

    /*<![CDATA[*/
    contextPath = /*[[@{/}]]*/ '';
    /*]]>*/

</script>
<script>
    layui.use(['laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'slider'],
        function() {
            var table = layui.table; //表格
            var layer = layui.layer;
            var $ = layui.jquery;

            $("#submit").on("click", function(){
                var  param = form2Json('add_app_form');
                if(param.appName===undefined || ""===param.appName.trim()){
                    layer.msg('应用名为必填');
                    return;
                }
                if(param.logPath===undefined || ""===param.logPath.trim()){
                    layer.msg('日志路径为必填');
                    return;
                }
                var index = layer.msg('创建中...', {icon: 16,shade: 0.5});
                $.ajax({
                    type: "POST",
                    url: contextPath + "api/apps/add_app.do",
                    data: JSON.stringify(form2Json('add_app_form')),
                    dataType: "json",
                    contentType: "application/json;charset=utf-8",
                    success: function(response){
                        index = layer.msg('创建成功', {time :1000});
                        setTimeout(function () {
                            window.parent.location.reload();
                        }, 500);
                    }
                });
            });


            function form2Json(formId) {
                var paramArray = $('#' + formId).serializeArray();
                var jsonObj={};
                $(paramArray).each(function(){
                    jsonObj[this.name]=this.value;
                });
                // json对象再转换成json字符串
                //return JSON.stringify(jsonObj);
                return jsonObj;
            }
        }
    )
</script>

</body>
</html>
